/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import model.pojo.UserProfile;
import model.pojo.util.helper;

/**
 *
 * @author Yasith
 */
public class fbUserProfile {

    public static UserProfile getUserProfile(int userID){
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("UserProfile.findByUserID");
        query.setParameter("userID", userID);
        UserProfile up;
        try {
            up = (UserProfile) query.getSingleResult();
        } catch (NoResultException ex) {
            up = new UserProfile();
            up.setUserID(userID);
            newEm.persist(up);
        }
        newTx.commit();
        newEm.close();

        return up;
    }
    
    public static UserProfile saveUserProfile(UserProfile up) {

        EntityManager em = helper.getEMF().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        UserProfile up2 = em.find(UserProfile.class, up.getUserID());
        up2.setAboutMe(up.getAboutMe());
        up2.setAge(up.getAge());
        up2.setCreationDate(up.getCreationDate());
        up2.setGender(up.getGender());
        up2.setLastUpdatedDate(up.getLastUpdatedDate());
        up2.setMyInterest(up.getMyInterest());
        up2.setPhotoLink(up.getPhotoLink());
        up2.setVideoLink(up.getVideoLink());
        
        em.persist(up2);
        tx.commit();
        em.close();
        return up2;
    }
}
